<template>
	<view class="container">
         <view class="segmented-control">
			<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="button">
			</uni-segmented-control>
		</view>
		<view class="content">
			<view v-if="current === 0">
				<uni-list>
					<uni-list-item v-for="(item, index) in list1" :key="index" :title="item.nodeName"
					    :note="'流程名称：' + item.flowName + '，提交人：' + item.submitterName + '，提交时间：' + item.submitTime" 
					    showArrow rightText="处理" link="navigateTo" 
						:to="`./deal?processId=`+item.processId+`&workId=`+item.workId+`&flowId=`+item.flowId+`&nodeName=`+item.nodeName+`&creator=`+item.creator" 
						@click="onClick" />
				</uni-list>
			</view>
			<view v-if="current === 1">
				<uni-list>
					<uni-list-item v-for="(item, index) in list2" :key="index" :title="item.nodeName"
					    :note="'流程名称：' + item.flowName + '，发起人：' + item.creatorName + '，处理时间：' + item.dealTime" 
					    showArrow rightText="明细" link="navigateTo" :to="`./details?workId=`+item.workId" @click="onClick" />
				</uni-list>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getProcessList,
	} from "@/utils/api/api.js"
	
	export default {
		data() {
			return {
				current: 0,
				items: ['待办', '已办'],
				list1: [], // 待办列表
				list2: [], // 已办列表
			}
		},
		onShow() {
			uni.$on('refreshData', this.getProcess); // 监听全局事件并执行刷新方法
		},
		onHide() {
			uni.$off('refreshData', this.getProcess); // 页面隐藏时取消监听，避免内存泄漏
		},
		onReady() {
			this.init()
		},
		methods: {
            onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
					this.getProcess();
				}
			},
			onClick(e) {
				console.log('执行click事件', e.data)
				// uni.showToast({
				// 	title: '点击反馈'
				// });
			},
			//第一次调用
			init() {
				this.getProcess();
			},
			getProcess() {
				this.list1 = [];
				this.list2 = [];
				//待办
				if (this.current == 0) {
					let data = {
						status: 1,
					}
					getProcessList(data).then(res => {
						console.log(res)
						if (res.code == 1) {
							this.list1 = res.data;
						} else {
							uni.showToast({
								title: res.msg,
								duration: 2000,
								mask: true,
								icon: 'none'
							});
						}
					})
				} else if (this.current == 1) {
					let data = {
						status: 0,
					}
					getProcessList(data).then(res => {
						if (res.code == 1) {
							res.data.forEach((s) => { // 过滤特殊定制流程
								if (!s["flowName"].startsWith("项目") && !s["flowName"].startsWith("需求") && !s["flowName"].startsWith("问题")){
									this.list2.push(s);
								}
							})
						} else {
							uni.showToast({
								title: res.msg,
								duration: 2000,
								mask: true,
								icon: 'none'
							});
						}		
					})
				}
			},
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>
